package rickyGame.game.action
{
	import flash.geom.Point;
	
	import ricky.animate.TimeServer;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.SkillObject;
	import rickyGame.game.action.ActionHelper;
	import rickyGame.game.skill.SkillData;
	
	/**
	 * 放箭行动
	 */
	public class ShootAct extends Act2D
	{
		public function ShootAct(actor:Actor,skill:SkillData,target:Actor,point:Point=null)
		{
			super(actor);
			_skill=skill;
			this._point=point;
			this._target=target;//攻击目标
		}
		
		override public function begin():void
		{
			_state=1;
			_x=actor.X;
			_y=actor.Y;
			if(_point)
			{
				_x=_point.x;
				_y=_point.y+5;
			}
			if(_target)
			{
				_x=_target.x;
				_y=_target.Y+5;
			}
			
			_interval=_skill.effectInterval;
			
			_startTime=TimeServer.now();
			_last=_startTime;
			_endTime=_startTime+_skill.continueTime;
			
			if(_skill.continueTime==0)
			{
				createSkillObj();
				this.complete();
			}
		}
		
		override public function action(timeline:Number):void
		{
			if(_state<1) //如果没有初始化
				return;
			
			var dt:Number=timeline-_last;
			if(dt>= _interval)//
			{
				_last=timeline;
				createSkillObj();
			}
			
			if(timeline>=_endTime)
			{
				this.complete();
			}
		}
		
		override public function end():void
		{
//			JDebug.trace("5:shootAct:end"+actor);

			super.end();
		}
		
		override public function dispose():void
		{
//			JDebug.trace("6:shootAct:销毁"+actor);

			super.dispose();
			_skillObj=null;
			_skill=null;
		}
		
		protected function createSkillObj():void
		{
			_count++;
			JDebug.trace("4:ShootAct创建箭塔对象:["+actor+"]"+_count+"次("+_skill);
			_skillObj=new SkillObject(Fighter(actor),_skill);
			_skillObj.X=actor.x;
			_skillObj.Y=actor.y;
			_skillObj.speed=_skill.parameter1;
			this._skillObj.graphic.wearClothes(_skill.model);
			this._skillObj.graphic.fps=7;
			
			
			var ani:Act2D;
			var anipro:Array=_skill.animProps;
			
			var type:String=_skill.parameter;
			switch(type)
			{
				case "line"://直线
					ani=new LineAct(_skillObj,new Point(_x,_y),_skill);
					break;
				case "angleline"://角度
					var dx:Number=_x-this._actor.x;
					var dy:Number=_y-this._actor.y;
					var angle:Number=Math.atan2(dy,dx);//角度
					angle=angle+_lastAngle/(180/Math.PI);
					var _vx:Number=Math.cos(angle)*this._actor.speed+_skillObj.x;
					var _vy:Number=Math.sin(angle)*this._actor.speed+_skillObj.y;
					ani=new LineAct(_skillObj,new Point(_vx,_vy),_skill);
					_lastAngle+=20;
					break;
				case "follow":
					ani=new FollowHitAct(_skillObj,_target,_skill);
					break;
			}
			
			if(ani)
				_skillObj.addAct(ani);
			
			GameState.inst.addGameObject(_skillObj);
		}
		//
		private var _lastAngle:Number=-20;//上个角度
		private var _x:Number;
		private var _y:Number;
		private var _point:Point;//目标点
		private var _target:Actor;//目标
		private var _skill:SkillData;//技能数据
		private var _skillObj:SkillObject;//技能对象
		
		private var _startTime:Number;//开始时间毫秒数
		private var _endTime:Number;//持续时间毫秒数
		private var _last:Number;//上次时间
		private var _interval:Number;//执行间隔
		
		private var _count:int=0;
	}
}